class ApplicationController < ActionController::Base

  before_filter :set_locale

  protect_from_forgery

  helper :all
  helper_method :current_user_session, :current_user
  #filter_parameter_logging :password, :password_confirmation

  rescue_from CanCan::AccessDenied do |exception|
     redirect_to '/403.html', :alert => exception.message
  end

  private

  def current_user_session
    return @current_user_session if defined?(@current_user_session)
    @current_user_session = UserSession.find
  end

  def current_user
    return @current_user if defined?(@current_user)
    @current_user = current_user_session && current_user_session.record
  end

  def require_user
    unless current_user
    store_location
    flash[:notice] = "You must be logged in to access this page"
    redirect_to new_user_session_url
    return false
    end
  end

  def require_no_user
    if current_user
    store_location
    flash[:notice] = "You must be logged out to access this page"
    redirect_to :action => :index , :controller => :main
    return false
    end
  end

  def store_location
    session[:return_to] = request.fullpath
  end

  def redirect_back_or_default(default)
    redirect_to(session[:return_to] || default)
    session[:return_to] = nil
  end

  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
  end

  def default_url_options(options={})
    #logger.debug "default_url_options is passed options: #{options.inspect}\n"
    { :locale => I18n.locale }
  end

end
